<?php

require_once "../../php/config/config.php";
require_once "../model/adminDb.php";
$AdminDb = new AdminDb();
$name = $_SESSION['name'];
$url = $_SERVER["QUERY_STRING"];
$id = str_replace("id=", '', $url);
$dat = $AdminDb->query_userName($name);
$card = $dat[0]['card'];
$user_Id=$dat[0]['id'];
$userId = $AdminDb->queryBug_id($id);
$newName = $userId[0]['newName'];
$status = $userId[0]['status'];
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" href="../../static/js/layui/css/layui.css">
    <link rel="stylesheet" href="../../static/css/view.css"/>
    <link rel="">
    <title></title>
    <style>
        #test10 {
            margin-left: 100px;
        }

        .imgCss {
            margin-left: 20px;
            height: 200px;
            cursor: pointer;
        }

        #status_title {
            font-size: 28px;
        }

        .status {
            width: 30%;
            text-align: center;
            /*background-color: green;*/
        }

        #status_div {
            background-color: #F0F2F5;
            display: flex;
            color: white;
            flex-direction: row;
        }

        #status_box {
            display: flex;
            flex-direction: row;
            width: 100%;
        }

        #publish {
            float: right;
            /*position: absolute;*/
            right: 80px;
            margin-top: 50px;
        }

        #create_time {
            float: right;
            right: 0;
            opacity: 0.5;
        }

        #itemComment {
            background-color: transparent;
        }

        #itemBox {
            padding: 20px;
        }

        #comment {
            margin-left: 20px;
        }

        .footer {
            width: 100%;
            height: 150px;
            line-height: 150px;

        }

        .footerItem {
            width: 100%;
            height: 50px;
            bottom: 0;
            line-height: 150px;
            text-align: center;
        }

        #commentBox {
            border: 1px white dashed;
        }

        #comment_itemBox {
            margin-top: 22px;
        }

    </style>
</head>
<body class="layui-view-body">
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-header">新增bug</div>
            <form class="layui-form layui-card-body" action="" lay-filter="example">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品</label>
                    <div class="layui-input-block">
                        <select name="product" id="product" lay-filter="product" disabled>
                            <option value=""></option>
                            <option value="易汉语">易汉语</option>
                            <option value="古筝学生端">古筝学生端</option>
                            <option value="古筝教师端">古筝教师端</option>
                            <option value="欢乐中国节">欢乐中国节</option>
                            <option value="综合管理部周报">综合管理部周报</option>
                            <option value="库存平台">库存平台</option>
                            <option value="BUG平台">BUG平台</option>
                            <option value="易汉语后台">易汉语后台</option>
                            <option value="其他">其他</option>

                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">平台</label>
                    <div class="layui-input-block" lay-filter="pingtai" id="pingtai">

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">负责人</label>
                    <div class="layui-input-block">
                        <select name="person" id="person" lay-filter="person" disabled>
                            <option value=""></option>

                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">bug标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" required lay-verify="title" disabled placeholder="请输入标题"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">版本号</label>
                    <div class="layui-input-block">
                        <input type="number" name="version" required lay-verify="version" id="version" disabled
                               placeholder="请输入版本号"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" id="desBox">
                    <label class="layui-form-label">正文</label>
                    <div class="layui-input-block">
                        <textarea name="desc" disabled class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">内容</label>
                    <br>
                    <div class="layui-input-block" id="content">


                    </div>
                </div>
                <br>
                <div class="layui-form-item" style="margin-top: 20px">
                    <div class="layui-inline">
                        <label class="layui-form-label">新建人</label>
                        <div class="layui-input-inline">
                            <input name="new_name" class="layui-input" id="new_name" type="text" disabled
                                   autocomplete="off"
                                   value="" lay-verify="new_name">
                        </div>

                    </div>
                </div>
                <br>
                <input id="user_id" value="<?php echo $user_Id ?>" style="display: none">
                <input id="auther_name" value="<?php echo $name ?>" style="display: none">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>状态</legend>
                </fieldset>
                <div id="status_div">

                    <div class="status" id="one"><span>测试完成 30%</span></div>
                    <div class="status" id="two"><span>修改完成 60%</span></div>
                    <div class="status" id="three"><span>完成100%</span></div>


                </div>
                <div id="statusBox">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                        <legend>编辑状态</legend>
                    </fieldset>
                    <div class="layui-upload layui-row" id="status_box">

                        <?php
                        if ($status == "0") {
                            echo "  <a id='status0' class=\"layui-btn  layui-btn-danger layui-col-xs2\" onclick=\"editStatus(3)\" lay-submit lay-filter=\"editStatus\">待完成</a>";
                            echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(1)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";
                        } elseif ($status == "1") {

                            if ($card == 1 || $name == $newName) {
                                echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(1)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";
                                echo "  <a id='status0' class=\"layui-btn layui-btn-primary layui-col-xs2\" onclick=\"editStatus(0)\" lay-submit lay-filter=\"editStatus\">驳回</a>";
                            }
                        } elseif ($status == "2") {

                            if ($card == 1 || $name == $newName) {
                                echo "  <a id='status0' class=\"layui-btn layui-btn-primary layui-col-xs2\" onclick=\"editStatus(0)\" lay-submit lay-filter=\"editStatus\">驳回</a>";
                            }
                        } elseif ($status == "3") {


                            echo " <a class=\"layui-btn layui-btn-primary layui-col-xs2 \" id=\"status2\" onclick=\"editStatus(1)\" lay-submit lay-filter=\"editStatus\">完成解决</a>";

                        }


                        ?>
                    </div>
                </div>
            </form>

        </div>
    </div>


</div>
<div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>发表评论</legend>
    </fieldset>

    <div class="layui-card-body" id="commentBox">
        <script id="container" name="content" type="text/plain">

        </script>
        <button class="layui-btn" id="publish"><i class="layui-icon">&#xe609;</i> 发表</button>
    </div>
    <br>

    <div id="itemBox">

    </div>
</div>
<div class="footer">
    <div class="footerItem">@2019 04 buglist平台</div>
</div>
<!-- 配置文件 -->
<script type="text/javascript" src="../../static/uedit/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="../../static/uedit/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('container', {
        toolbars: [
            ['snapscreen', 'fullscreen', 'undo', 'redo', 'bold', 'underline', 'insertvideo', 'forecolor', 'scrawl', 'emotion']
        ],
        maximumWords: 1000,
        initialFrameHeight: 240,
        autoHeightEnabled: true,
        autoFloatEnabled: true
    });
</script>
<script type="text/javascript" src="../../static/js/jquery-3.3.1.min.js"></script>
<script src="../../static/js/layui/layui.all.js"></script>
<script src="../../static/js/config.js"></script>
<script>
    var regex2 = /\"(.+?)\"/g;   // [] 中括号
    var url = document.location.toString();
    var id = url.split("=")[1];
    $(document).ready(function () {

        $("#publish").click(function () {
            $content = UE.getEditor('container').getPlainTxt();
            publish_comment($content);
        });
        get_comment(id);    query(id);
    });

    //获取bug数据函数
    function query(id) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_bug.php',
            data: {
                "id": id,
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {//获取成功
                    assignment(res.data);
                }
            },
            error: function (res) {
                layer.msg("连接失败！");
            }
        });
    }

    function assignment(value) {
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //数据提取
            $product = value[0].product;
            $person = value[0].person;
            $title = value[0].title;
            $newName = value[0].newName;
            $desc = value[0].desction;
            $version = value[0].version;
            $imgData = value[0].image;
            $imgData = $imgData.replace("[", "").replace("]", "");
            $imgData1 = $imgData.split(",");

            $content = value[0].content;
            $("#content").html($content);
            //显示图片
            for ($i = 0; $i < $imgData1.length; $i++) {
                $imgUrl = $imgData1[$i].replace("\"", "").replace("\"", "").split("php/")[1];

                $domain = window.location.href.split("view")[0];

                if ($imgUrl == undefined) {

                    $li = $("<span>暂无！</span>");
                    $("#imgDiv").append($li);

                }
                else {
                    $imgUrl = $domain + $imgUrl;
                    $imgId = "img" + $i;

                    $li = $("<img class='imgCss' id=\"" + $imgId + "\" onclick='show_img(\"" + $i + "\",\"" + $imgUrl + "\")' src=\"" + $imgUrl + "\" />");
                    $("#imgDiv").append($li);
                }

            }
            //显示状态
            $status = value[0].status;
            if ($status == "0") {//未完成状态
                $("#status_title").css({"color": "red"});
                $("#one").css({"background": "#009688"});

            }
            else if ($status == "1") {//
                console.log($status);

                $("#status_title").css({"color": "white"});
                $("#one").css({"background": "#009688"});
                $("#two").css({"background": "#009688"});


            }
            else if ($status == "2") {
                $("#status_div").css({"background-color": "#009688"});
                $("#status_title").css({"color": "white"});
                $("#status2").css({"display": "none"});

            }
            else if ($status == "3") {
                $("#one").css({"background": "#FF5722"});
                $("#two").css({"background": "#FF5722"});
                $("#two").html("待完成");

            }

            //表单初始赋值
            form.val('example', {
                "product": $product,
                "person": $person,
                "title": $title,
                "desc": $desc,
                "new_name": $newName,
                "version": $version,
            });
            if ($desc == "") {
                $("#desBox").css({"display": "none"});
            }

            //负责人赋值
            selectPerson($person);

            //平台赋值
            $platfrom = value[0].platfrom;
            $platfrom = $platfrom.split(",");
            for ($i = 0; $i < $platfrom.length; $i++) {
                $li = $(" <input type=\"checkbox\" name=\"platfrom\"  value=\"" + $platfrom[$i] + "\" title=\"" + $platfrom[$i] + "\" disabled>");
                $("#pingtai").append($li);
            }
        });
    }

    //查看图片
    function show_img(id, url) {
        $id = "#img" + id;
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ['90%', '90%'],
            shadeClose: true,
            skin: 'yourclass',
            content: '<img src=\'' + url + '\' style="width: 100%" alt="图片预览" />'
        });
    }

    //负责人选择框
    function selectPerson(person) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_user.php',
            data: {},
            dataType: 'json',
            success: function (res) {


                $dat = res.data;
                $count = $dat.length;
                var select_person = document.getElementById("person");
                for ($i = 0; $i < $count; $i++) {
                    if ($dat[$i].name == person) {
                        select_person.innerHTML += "<option value='" + $dat[$i].name + "'  selected=\"\">" + $dat[$i].name + "</option>";
                    }
                    else {
                        select_person.innerHTML += "<option value='" + $dat[$i].name + "' >" + $dat[$i].name + "</option>";
                    }


                }
                layui.use('form', function () {
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                    form.render('select', 'select_person'); //刷新select选择框渲染
                });


            },
            error: function (res) {
                console.log("加载失败");
            }


        });


    }

    //完成解决
    function editStatus(val) {


        $.ajax(
            {
                type: 'post',
                cache: 'false',
                url: '../controller/edit_status.php',
                data: {
                    "id": id,
                    "val": val,
                },
                dataType: 'json',
                success: function (res) {
                    layer.msg(res.message);
                    if (res.code == 200) {
                        window.location.reload();
                    }
                },
                error: function (res) {
                    layer.msg("更新失败");
                }
            }
        )


    }

    //发表评论
    function publish_comment(content) {
        console.log(content.length);
      if(content.length<3){
          layer.msg("您还未评论哦",{icon:5});
      }
      else {
          $auther_name = $("#auther_name").val();
          $user_id=$("#user_id").val();
          $time = getNowFormatDate();


          $.ajax(
              {
                  type: 'post',
                  cache: 'false',
                  url: '../controller/add_comment.php',
                  data: {
                      "user_id": $user_id,
                      "bug_id":id,
                      "series":1,
                      "content": content,
                  },
                  dataType: 'json',
                  success: function (res) {
                      layer.msg(res.message);
                      if (res.code == 200) {
                          $li = "<div class=\"layui-col-md12\" id='comment_itemBox'>\n" +
                              "            <div class=\"layui-card\" id=\"itemComment\" >\n" +
                              "                <div class=\"layui-card-header\"><span>" +
                              "作者：" + $auther_name + "" +
                              "</span><a id=\"auther_name\"></a><a id=\"create_time\">" +
                              "" + $time +
                              "</a> </div>\n" +
                              "                <div class=\"layui-card-body\"  id=\"comment\">\n" +
                              content +
                              "                </div>\n" +
                              "            </div>\n" +
                              "        </div>";


                          $("#itemBox").append($li);
                      }
                  },
                  error: function (res) {
                      layer.msg("评论失败",{icon:5});
                  }
              }
          );







      }




    }

    //判断是否登录与填写
    //获取评论
    function get_comment(id) {
        $.ajax({
            type: 'post',
            cache: 'false',
            url: '../controller/query_comment.php',
            data: {
                "bug_id": id,
            },
            dataType: 'json',
            success: function (res) {
                $data=res.data;
                if (res.code == 0) {//获取成功
                    for($i=0;$i<$data.length;$i++){

                        $li = "<div class=\"layui-col-md12\" id='comment_itemBox'>\n" +
                            "            <div class=\"layui-card\" id=\"itemComment\" >\n" +
                            "                <div class=\"layui-card-header\"><span>" +
                            "昵称：" + $data[$i]['name'] + "" +
                            "</span><a id=\"auther_name\"></a><a id=\"create_time\">" +
                            "" + to_date($data[$i]['addtime']) +
                            "</a> </div>\n" +
                            "                <div class=\"layui-card-body\"  id=\"comment\">\n" +
                            $data[$i]['content'] +
                            "                </div>\n" +
                            "            </div>\n" +
                            "        </div>";


                        $("#itemBox").append($li);
                    }

                }
            },
            error: function (res) {
              console.log("获取评论失败！或无评论");
            }
        });
    }


</script>
</body>
</html>